package com.ruoyi.media.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.List;

import com.ruoyi.common.constant.MediaConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.media.domain.vo.PlansVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.media.mapper.PlansMapper;
import com.ruoyi.media.domain.Plans;
import com.ruoyi.media.service.IPlansService;

/**
 * 个人计划Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-03-11
 */
@Slf4j
@Service
public class PlansServiceImpl implements IPlansService 
{
    @Autowired
    private PlansMapper plansMapper;

    /**
     * 查询个人计划
     * 
     * @param planId 个人计划主键
     * @return 个人计划
     */
    @Override
    public PlansVO selectPlansByPlanId(Long planId)
    {
        Plans plans = plansMapper.selectPlansByPlanId(planId);
        //1.计算剩余时间
        LocalDateTime startDate = plans.getStartDate();
        LocalDateTime endDate = plans.getEndDate();
        if ( startDate == null || endDate == null) {
            throw new BaseException("开始时间和结束时间不能为空");
        }
        //周期时间
        long cycleTime =  ChronoUnit.DAYS.between(
                startDate.toLocalDate(),
                endDate.toLocalDate()
        );
        //剩余时间
        long daysUntilExpiry = ChronoUnit.DAYS.between(LocalDate.now(), endDate.toLocalDate());
        log.debug("周期时间差距：{}", cycleTime);
        log.debug("到期时间：{}", daysUntilExpiry);
        //2.拷贝数据到PlansVO
        PlansVO plansVO = new PlansVO();
        BeanUtils.copyProperties(plans,plansVO);
        plansVO.setCycleTime(cycleTime);
        plansVO.setLeftTime(daysUntilExpiry);
        //3.判断是否过期（默认未过期）
        //3.1 过期
        if(daysUntilExpiry <= MediaConstants.IS_EXPIRY_NO){
            plansVO.setIsExpiry(MediaConstants.IS_EXPIRY_YES);
        }

        //4.返回数据
        return plansVO;
    }

    /**
     * 查询个人计划列表
     * 
     * @param plans 个人计划
     * @return 个人计划
     */
    @Override
    public List<Plans> selectPlansList(Plans plans)
    {
        return plansMapper.selectPlansList(plans);
    }

    /**
     * 新增个人计划
     * 
     * @param plans 个人计划
     * @return 结果
     */
    @Override
    public int insertPlans(Plans plans)
    {

//        获取当前用户id
        //获取用户id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        plans.setUserId(user.getUserId());
        plans.setCreateTime(DateUtils.getNowDate());
        plans.setUpdateTime(DateUtils.getNowDate());

        return plansMapper.insertPlans(plans);
    }

    /**
     * 修改个人计划
     * 
     * @param plans 个人计划
     * @return 结果
     */
    @Override
    public int updatePlans(Plans plans)
    {
        plans.setUpdateTime(DateUtils.getNowDate());
        //1、计划结束日期在当前日期之前
        LocalDateTime endDate = plans.getEndDate();
        boolean isBefore = endDate.toLocalDate().isBefore(LocalDate.now());
        Long progress = plans.getProgress();
        if(isBefore && progress!=100L){
            //1.1 计划过期
            plans.setIsExpiry(MediaConstants.IS_EXPIRY_YES);
        }else{
            //1.2 未过期
            plans.setIsExpiry(MediaConstants.IS_EXPIRY_NO);
        }

        return plansMapper.updatePlans(plans);
    }

    /**
     * 批量删除个人计划
     * 
     * @param planIds 需要删除的个人计划主键
     * @return 结果
     */
    @Override
    public int deletePlansByPlanIds(Long[] planIds)
    {
        return plansMapper.deletePlansByPlanIds(planIds);
    }

    /**
     * 删除个人计划信息
     * 
     * @param planId 个人计划主键
     * @return 结果
     */
    @Override
    public int deletePlansByPlanId(Long planId)
    {
        return plansMapper.deletePlansByPlanId(planId);
    }

    @Override
    public void BatchUpdatePlans(List<Plans> plansList,int isExpiry) {

        plansMapper.BatchUpdatePlans(plansList,isExpiry);
    }
}
